package com.splic.api.controller.product;

import com.splic.common.core.controller.BaseController;
import com.splic.common.core.domain.ResponseObj;
import com.splic.common.exception.BusinessException;
import com.splic.product.domain.Work;
import com.splic.product.service.WorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 事件同步Controller
 * 
 * @author singe
 * @date 2023-06-18
 */
@RestController
@RequestMapping("/api/work")
@Slf4j
public class ApiWorkController extends BaseController
{

    @Autowired
    private WorkService workServiceImpl;

    /**
     * 查询同步
     *
     * @return
     */
    @GetMapping("/judgeSync")
    public ResponseObj<Work> judgeSync(Work workReq) {
        try{
            Work work = workServiceImpl.judgeSync(workReq);
            return ResponseObj.SUCCESS(work);
        } catch (BusinessException e){
            if (e.getCode() != -100) {
                return ResponseObj.ERROR(e.getMessage());
            } else {
                return ResponseObj.ERROR(e.getMessage(), String.valueOf(e.getCode()));
            }
        } catch (Exception e){
            log.error("同步判断失败:{}",e);
            return ResponseObj.ERROR("同步判断失败。");
        }
    }

    /**
     * 同步结束
     *
     * @return
     */
    @GetMapping("/finishSync")
    public ResponseObj<Integer> finishSync(Work workReq) {
        try{
            return ResponseObj.SUCCESS(workServiceImpl.finishSync(workReq));
        } catch (BusinessException e){
            return ResponseObj.ERROR(e.getMessage(), String.valueOf(e.getCode()));
        } catch (Exception e){
            log.error("结束同步失败:{}",e);
            return ResponseObj.ERROR("结束同步失败。");
        }
    }
}
